<?php
require_once "includes/config.php";
require_once "includes/functions.php";
require_once('AmazonAPI/.config.inc.php');
require_once('AmazonAPI/CBUI/CBUIMultiUsePipeline.php');
session_start();
$sessid=session_id();
$amount=0;
$sqlCart="select * from tempcart tt  where tt.session_id='".$sessid."'";
$resCart=mysql_query($sqlCart);
$rowCart=mysql_num_rows($resCart);

//arrays
$recipient_token_arr = array();
$recipient_amount_arr = array();
$tempID_arr = array();
$item_arr = array();
$itemname_arr = array();
$qty_arr = array();
$temp_tax_arr = array();
$temp_item_code=array();

$extra=0;

while($rows=mysql_fetch_array($resCart))
{
	if((int)$rows['itemamt']<10)
	{	
		if((int)$rows['is_online']==0)
		{
			$extra=number_format((int)$rows['itemamt']+(int)$rows['sales_tax'],2);
		}
		else
		{
			$extra=number_format((int)$rows['itemamt']+(int)$rows['sales_tax']+(int)$rows['shipping'],2);
		}
	}
	else
	{
		$extra=0;
	}	
	if((int)$rows['is_online']==0)
	{
		$payable_amt = (($rows['qty']*$rows['itemamt'])+number_format((number_format($rows['sales_tax'],2)*($rows['itemamt'])/100),2));
	}
	else
	{
		$payable_amt = (($rows['qty']*$rows['itemamt'])+number_format((number_format($rows['sales_tax'],2)*($rows['itemamt'])/100),2) + $rows['shipping']);
	}
	
	$total_amount[] = $payable_amt;
	
	//get user id form item id
	if($rows['isWhole']=='0')
	{
		$item_q = "SELECT * FROM tbl_items WHERE item_id= '".$rows['itemid']."'";
		$item_r = mysql_query($item_q);
		$item_row = mysql_fetch_array($item_r);
		
		$user_q = "SELECT * FROM tbl_users WHERE user_id = '".$item_row['seller_id']."'";
		$user_r = mysql_query($user_q);
		$user_row = mysql_fetch_array($user_r);
	}
	else
	{
		$item_q = "SELECT * FROM tbl_wholeseller_product WHERE product_id= '".$rows['itemid']."'";
		$item_r = mysql_query($item_q);
		$item_row = mysql_fetch_array($item_r);
		
		$user_q = "SELECT * FROM tbl_users WHERE user_id = '".$item_row['posted_by']."'";
		$user_r = mysql_query($user_q);
		$user_row = mysql_fetch_array($user_r);
		$isWhole=true;
	}
	//get user data by user id
	
	//echo $user_row['tokenID']; 
//	if((int)$extra=0)
//	{
//		$recipient_token_arr[] = $user_row['tokenID'];
//	}
//	else 
	
	//if((int)$extra!=0 && (int)$extra<10)
//	{
//		$recipient_token_arr[] = $user_row['tokenID1'];
//		//echo "hi";exit();
//	}
//	else
//	{
//		$recipient_token_arr[] = $user_row['tokenID'];
//	}

	$recipient_token_arr[] = $user_row['tokenID'];
	//echo $user_row['tokenID'];
	//print_r($recipient_token_arr); exit;
	$recipient_amount_arr[] = $payable_amt;
	$tempID_arr[] = $rows['tempid'];
	$item_arr[] = $rows['itemid'];
	$itemname_arr[] = $rows['itemname'];
	$qty_arr[] = $rows['qty'];
	$temp_tax_arr[] = number_format($rows['sales_tax'],2);
	$temp_item_code[]=$rows['item_code'];
	
	//$ordered_by_arr[] =  $_SESSION['user_id_sess'];
	
	
	
}
//print_r($item_arr);
//echo "<br/>".$item_arr[0];
//echo "<br/>".$rows['itemid'];
//echo $payable_amt;


$_SESSION['recipient_tokens'] = $recipient_token_arr;
//print_r($recipient_token_arr);
$_SESSION['recipient_amounts'] = $recipient_amount_arr;
$_SESSION['tempID_arr'] = $tempID_arr;
$_SESSION['item_arr'] = $item_arr;
$_SESSION['itemname_arr'] = $itemname_arr;
$_SESSION['qty_arr'] = $qty_arr;
$_SESSION['sales_tax_arr'] =$temp_tax_arr;
$_SESSION['item_code'] =$temp_item_code;

$amount=number_format(array_sum($total_amount),2);

$sqlprofile="select * from tbl_users where user_id='".$_SESSION['user_id_sess']."'";
$resprofile=mysql_query($sqlprofile);
$rowprofile=mysql_fetch_array($resprofile);

$pipeline = new Amazon_FPS_CBUIMultiUsePipeline(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
//echo $amount;
if($isWhole==true)
{
	$pipeline->setMandatoryParameters("swapen".rand(),  
		H_ROOT."process_amazon1.php", $amount);
}
else
{
	$pipeline->setMandatoryParameters("swapen".rand(),  
		H_ROOT."process_amazon.php", $amount);
}		
$pipeline->setRecipientTokenList(false, $recipient_token_arr);

//optional parameters
$pipeline->addParameter("paymentMethod", "CC");
$re_URL = $pipeline->getUrl();
header("location:".$re_URL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Swapen.com - Buy and Sell</title>
<style type="text/css">
<!--
.style1 {font-weight: bold}
.style2 {color:#fefefe; font-size:12px; font-family: "Trebuchet MS"; background-color:#000000;}
.style3 {font-size:14px; color: #4a4a4a; font-family: "Trebuchet MS";}
-->
</style>

</head>

<body>
<div align="center" class="style1"><span class="style2">Please wait, you are being redirected to selected payment gateway</span><span class="style3">.</span></div>

</body>
</html>
